package com.shoesmail.service.impl;

import com.shoesmail.domain.dto.ShoesTypeDto;
import com.shoesmail.domain.dto.UserOrderDetailDto;
import com.shoesmail.domain.pojo.Address;
import com.shoesmail.domain.vo.JsonResultVo;
import com.shoesmail.domain.vo.user.UserOrderVo;
import com.shoesmail.enums.OrderState;
import com.shoesmail.mapper.AddressMapper;
import com.shoesmail.mapper.OrderTabMapper;
import com.shoesmail.mapper.ShoeMapper;
import com.shoesmail.service.UserOrderService;
import com.shoesmail.utils.PhotoUtil;
import com.shoesmail.utils.StringUtils;
import com.shoesmail.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：CDB
 * @description：TODO
 * @date ：2025/9/18 11:07
 */
@Service
public class UserOrderServiceImpl implements UserOrderService {

    @Autowired
    private OrderTabMapper orderTabMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private PhotoUtil photoUtil;

    @Autowired
    private ShoeMapper shoeMapper;
    @Override
    public JsonResultVo<List<UserOrderVo>> getOrder() {
        String userId = UserContext.getUserId();
        if(StringUtils.isNull(userId)){
            return JsonResultVo.fail("请登录");
        }
        List<UserOrderDetailDto> orderById = orderTabMapper.getOrderById(Integer.valueOf(userId));
        List<UserOrderVo> orderVos = new ArrayList<>();
        for(UserOrderDetailDto orderDetailDto:orderById){
            UserOrderVo userOrderVo = new UserOrderVo();

            int addressId = orderDetailDto.getAddressId();
            String addressDetail = addressMapper.getAddressDetail(addressId);
            int colourId = orderDetailDto.getColourId();

            ShoesTypeDto shoe = shoeMapper.getShoe(orderDetailDto.getShoesId());


            String photoUrl = photoUtil.getPhotoUrl(shoe.getBrandId(), shoe.getCategoryId(), colourId);


            userOrderVo.setTime(orderDetailDto.getOrderTime());
            userOrderVo.setOrderState(OrderState.get(orderDetailDto.getState()));
            userOrderVo.setTotalMoney(orderDetailDto.getSumMoney());
            userOrderVo.setName(shoe.getName());
            userOrderVo.setPhotoUrl(photoUrl);
            userOrderVo.setAddress(addressDetail);
            orderVos.add(userOrderVo);
        }
        return JsonResultVo.success("成功",orderVos);

    }


}













